package misc;

import com.jme3.export.InputCapsule;
import com.jme3.export.JmeExporter;
import com.jme3.export.JmeImporter;
import com.jme3.export.OutputCapsule;
import com.jme3.export.Savable;
import com.jme3.math.FastMath;
import java.io.IOException;

/** A vector of three integers. */
public class Vector3i implements Savable {
    public static final Vector3i ZERO = new Vector3i(0,0,0);
    protected int x, y, z;

    public Vector3i() {}

    public Vector3i(int x, int y, int z) {
	this.x = x;
	this.y = y;
	this.z = z;
    }

    public int getX() {
	return x;
    }

    public void setX(int x) {
	this.x = x;
    }

    public int getY() {
	return y;
    }

    public void setY(int y) {
	this.y = y;
    }

    public int getZ() {
	return z;
    }

    public void setZ(int z) {
	this.z = z;
    }

    public Vector3i addX(int x) {
	this.x += x;
        return this;
    }

    public Vector3i addY(int y) {
	this.y += y;
        return this;
    }

    public Vector3i addZ(int z) {
	this.z += z;
        return this;
    }
    
    public float distance(Vector3i other) {
        return FastMath.sqrt(FastMath.sqr(other.getX()-this.getX()) +
                             FastMath.sqr(other.getY()-this.getY()) +
                             FastMath.sqr(other.getZ()-this.getZ()));
    }

    @Override
    public String toString() {
	return "[" + x + "," + y + "," + z + "]";
    }

    @Override
    public Vector3i clone() {
	return new Vector3i(x, y, z);
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Vector3i other = (Vector3i) obj;
        if (this.x != other.x) {
            return false;
        }
        if (this.y != other.y) {
            return false;
        }
        if (this.z != other.z) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 41 * hash + this.x;
        hash = 41 * hash + this.y;
        hash = 41 * hash + this.z;
        return hash;
    }

    public void write(JmeExporter ex) throws IOException {
        OutputCapsule capsule = ex.getCapsule(this);
        capsule.write(x, "x", 0);
        capsule.write(y, "y", 0);
        capsule.write(z, "z", 0);
    }

    public void read(JmeImporter im) throws IOException {
        InputCapsule capsule = im.getCapsule(this);
        x = capsule.readInt("x", 0);
        y = capsule.readInt("y", 0);
        z = capsule.readInt("z", 0);
    }
}
